//
//  MCMainViewController.swift
//  HXWeibo
//
//  Created by HX on 15/11/23.
//  Copyright © 2015年 HX. All rights reserved.
//

import UIKit

class MCMainViewController: UITabBarController
{
    override func viewDidLoad()
    {
        super.viewDidLoad()
        
//        tabBar.tintColor = UIColor.orangeColor()
                
        //: 首页
        let homeVC = MCHomeViewController()
        addChildViewController(homeVC, title: "首页", imageName: "tabbar_home")
        
        //: 消息
        let messageVC = MCMessageViewController()
        addChildViewController(messageVC, title: "消息", imageName: "tabbar_message_center")
        
        //: 占位控制器 (在中间占个位置，用来添加“➕”按钮)
        addChildViewController(UIViewController())
        
        //: 发现
        let discoverVC = MCDiscoverViewController()
        addChildViewController(discoverVC, title: "发现", imageName: "tabbar_discover")
        
        //: 我
        let profileVC = MCProfileViewController()
        addChildViewController(profileVC, title: "我", imageName: "tabbar_profile")
    }
    
    override func viewWillAppear(animated: Bool)
    {
        super.viewWillAppear(animated)
        
        //: 设置撰写按钮的frame
        let w = tabBar.bounds.size.width / 5
        let frame  = CGRect(x: w * 2 - 5, y: 0, width: w + 10, height: tabBar.bounds.height)
        
        composeBtn.frame = frame;
    }
    
    //自定义添加tabbar子控制器 (方法抽取)
    private func addChildViewController(controller: UIViewController, title: String, imageName: String)
    {
        controller.title = title
        controller.tabBarItem.image = UIImage(named: imageName)
        
        //: 拼接高亮图片名称
        let highlightImageName = imageName + "_highlighted"
        
        //: 让选中的图片使用原来的颜色，不要系统默认的渲染
        let highlightImage = UIImage(named: highlightImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        controller.tabBarItem.selectedImage = highlightImage
        
        //: 设置标题颜色
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Highlighted)
        
        addChildViewController(UINavigationController(rootViewController: controller))
    }
    // MARK: - 撰写按钮 懒加载
    lazy var composeBtn: UIButton =
    {
        let btn = UIButton()
        
        //: 设置参数
        //: 设置按钮的背景图片
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        //: 设置按钮图片
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        //: 添加到tabbar里面
        self.tabBar.addSubview(btn)
        
        //: 添加 撰写按钮的点击事件
        btn.addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    //: 撰写按钮的点击事件
    func composeClick()
    {
        print(__FUNCTION__)
    }
}































